package com.animationsp.acs.action;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.net.URLCodec;

import com.animationsp.acs.util.Constant;
import com.animationsp.acs.util.ErrorMap;
import com.animationsp.acs.util.MyActionSupport;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.config.Config;
import com.qiniu.api.net.CallRet;
import com.qiniu.api.net.EncodeUtils;
import com.qiniu.api.rs.BatchCallRet;
import com.qiniu.api.rs.EntryPath;
import com.qiniu.api.rs.RSClient;

public class RemoveFileAction extends MyActionSupport {
	private String key;
	
	private String[] delFiles;
	@Override
	public String execute() throws Exception {
		key = new String(key.getBytes("iso8859-1"),"utf-8"); // tomcat默认编码是iso8859-1，所以收到的是该编码的参数，先转成utf-8		
		System.out.println("delete key: "+key);
		
		Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
		RSClient client = new RSClient(mac);
		CallRet delRet = client.delete(Constant.BUCKET, key);
		System.out.println("delete "+delRet.statusCode + " " + delRet.response + " "+ delRet.exception);
		if(!delRet.ok()){
			this.setErr(ErrorMap.ERROR_MAP.get(delRet.statusCode) == null?"未知错误"+delRet.statusCode:ErrorMap.ERROR_MAP.get(delRet.statusCode));
		}
		return SUCCESS;
	}
	
	// %E6%98%AF%E5%99%A2%20%E7%9A%84 mine
	// %E6%98%AF%E5%99%A2%20%E7%9A%84 qiniu
	
	public String batchDelete() throws Exception{
		if(delFiles == null || delFiles.length ==0 ){
			this.setErr("没有文件要删除");
			return SUCCESS;
		}
		Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
        RSClient rs = new RSClient(mac);
        List<EntryPath> entries = new ArrayList<EntryPath>();
        
        for(String df : delFiles){
        	EntryPath e = new EntryPath();
        	e.bucket = Constant.BUCKET;
            e.key = df;
            entries.add(e);
        }
        

        BatchCallRet bret = rs.batchDelete(entries);
        System.out.println("delete "+bret.statusCode + " " + bret.response + " "+ bret.exception);
		if(!bret.ok()){
			this.setErr(ErrorMap.ERROR_MAP.get(bret.statusCode) == null?"未知错误"+bret.statusCode:ErrorMap.ERROR_MAP.get(bret.statusCode));
		}
        
		for(String s : delFiles){
			System.out.println("delete file:  "+s);
		}
        return SUCCESS;
	}

	public static void main(String[] args) {
		System.out.println(System.getProperty("file.encoding"));
		String entryURI = Constant.BUCKET + ":" + "中文4";
		// String url = Config.RS_HOST + "/delete/"+ EncodeUtils.urlsafeEncode(entryURI);
		System.out.println("url: "+entryURI.getBytes());
		for(byte b : entryURI.getBytes()){
			System.out.print(b);
		}
		
		Mac mac = new Mac("lQvrG3e4dV9w8szU32Dq0SdkcJMTVIRQ_QjGIZIz", "nygi6Ew2RLw3W0fzhBcjtEXg15_D0nIUgVcnA81l");
		RSClient client = new RSClient(mac);
		// CallRet delRet = client.delete("aspcs", "中文4");
		// System.out.println(delRet.statusCode + " " + delRet.response + " "+ delRet.exception);
		
		// 971151129911558-28-72-83-26-106-12152    main
		// 971151129911558-42-48-50-6052            tomcat
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String[] getDelFiles() {
		return delFiles;
	}

	public void setDelFiles(String[] delFiles) {
		this.delFiles = delFiles;
	}
	
}
